<?php
/**
* @file $Id: PrintSchedules.php 405 2007-01-22 21:10:19Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

/*Widgets('request');
Widgets('course');
*/
Widgets('all');
$extra['force_search'] = true;

if(!$_REQUEST['search_modfunc'] || $_REQUEST['search_modfunc']=='search' || $_FOCUS['modules_search'])
{
	DrawHeader(ProgramTitle());

	$extra['new'] = true;
	$extra['action'] .= "&_FOCUS_PDF=true";
	$extra['second_col'] .= '<TABLE><TR><TD align='.ALIGN_RIGHT.'>'._('Include only courses active as of').' </TD><TD>'.PrepareDate('','_include_active_date').'</TD></TR>';
	$extra['second_col'] .= '<TR><TD align='.ALIGN_RIGHT.'>'._('Marking Period').' </TD><TD><SELECT name=marking_period_id>';

	$mps = GetAllMP('FY',UserMP());
	$mps = explode(',',str_replace("'",'',$mps));

	foreach($mps as $mp)
		$extra['second_col'] .= '<OPTION value='.$mp.'>'.GetMP($mp);

	$extra['second_col'] .= '</SELECT></TD></TR></TABLE>';

	Widgets('document_template');
	Widgets('mailing_labels');
	Search('student_id',$extra);
}
else
{
	if($_REQUEST['month_include_active_date'])
	{
		$date = $_REQUEST['day_include_active_date'].'-'.$_REQUEST['month_include_active_date'].'-'.$_REQUEST['year_include_active_date'];
		$date_extra = 'OR (\''.$date.'\' >= sr.START_DATE AND sr.END_DATE IS NULL) ';
	}
	else
	{
		$date = DBDate();
		$date_extra = 'OR sr.END_DATE IS NULL';
	}
	$columns = array('PERIOD_ID'=>_('Period'),'COURSE_TITLE'=>_('Course'),'TEACHER_ID'=>_('Teacher'),'ROOM'=>_('Room'),'MARKING_PERIOD_ID'=>_('Term'),'DAYS'=>_('Meeting Days'));

	$count_RET = DBGet(DBQuery("SELECT DISTINCT DAYS FROM COURSE_PERIODS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
	if(count($count_RET)==1)
		unset($columns['DAYS']);
	$count_RET = DBGet(DBQuery("SELECT DISTINCT ROOM FROM COURSE_PERIODS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
	if(count($count_RET)==1)
		unset($columns['ROOM']);

	$extra['SELECT'] .= ',c.TITLE AS COURSE_TITLE,sr.COURSE_WEIGHT,cp.TITLE AS PERIOD_TITLE,sr.MARKING_PERIOD_ID,cp.ROOM,cp.DAYS,cp.TEACHER_ID,cp.PERIOD_ID';
	$extra['FROM'] .= ',COURSES c,COURSE_PERIODS cp,SCHEDULE sr,SCHOOL_PERIODS sp ';
	$extra['WHERE'] .= " AND cp.PERIOD_ID=sp.PERIOD_ID AND ssm.STUDENT_ID=sr.STUDENT_ID AND ssm.SYEAR=sr.SYEAR AND sr.COURSE_ID=c.COURSE_ID AND sr.COURSE_PERIOD_ID=cp.COURSE_PERIOD_ID  AND ('".$date."' BETWEEN sr.START_DATE AND sr.END_DATE $date_extra) ";

	if($_REQUEST['marking_period_id'])
	{
		$table = GetMPTable(GetMP($_REQUEST['marking_period_id'],'TABLE'));
		$mp_list = GetAllMP($table,$_REQUEST['marking_period_id']);
		$extra['WHERE'] .= " AND sr.MARKING_PERIOD_ID IN (".$mp_list.") ";
	}

	$extra['functions'] += array('MARKING_PERIOD_ID'=>'_getMP','TEACHER_ID'=>'GetTeacher','PERIOD_ID'=>'GetPeriod');
	$extra['group'] = array('STUDENT_ID');
	$extra['ORDER'] = ',sp.SORT_ORDER';
	if($_REQUEST['mailing_labels']=='Y')
		$extra['group'][] = 'ADDRESS_ID';

	$RET = GetStuList($extra);

	if(count($RET))
	{
		$handle = PDFStart();
		foreach($RET as $student_id=>$courses)
		{
			if($_REQUEST['mailing_labels']=='Y')
			{
				foreach($courses as $address)
				{
					echo '<BR><BR><BR>';
					unset($_FOCUS['DrawHeader']);
					DrawHeader(Config('TITLE').' '._('Student Schedule'));
					DrawHeader($address[1]['FULL_NAME'],$address[1]['STUDENT_ID']);
					DrawHeader($address[1]['GRADE_ID']);
					DrawHeader(GetSchool(UserSchool()));
					DrawHeader(ProperDate(DBDate()));

					echo '<BR><BR><TABLE width=100%><TR><TD width=50> &nbsp; </TD><TD>'.$address[1]['MAILING_LABEL'].'</TD></TR></TABLE><BR>';

					ListOutput($address,$columns,_('Course'),_('Courses'),array(),array(),array('center'=>false,'print'=>false));
					echo '<!-- NEW PAGE -->';
				}
			}
			else
			{
				unset($_FOCUS['DrawHeader']);
				DrawHeader(Config('TITLE').' '._('Student Schedule'));
				DrawHeader($courses[1]['FULL_NAME'],$courses[1]['STUDENT_ID']);

				ListOutput($courses,$columns,_('Course'),_('Courses'),array(),array(),array('center'=>false,'print'=>false));
				echo '<!-- NEW PAGE -->';
			}
		}
		PDFStop($handle);
	}
	else
		BackPrompt(_('No Students were found.'));
}

function _getMP($marking_period_id,$column)
{
	return GetMP($marking_period_id,'TITLE');
}
?>